﻿using System;
using UberChess.Searching;

namespace UberChess.Searching
{
    public class IterativeDeepening
    {
        private Worker _worker;
        private State result;
        private bool startNewThread;
        

        public IterativeDeepening(Side playerSide, State currentState, int wantedSearchLevel)
        {
            startNewThread = true;
            _worker = new Worker(playerSide, currentState, wantedSearchLevel);
            _worker.Finished += new ThreadFinished(_worker_Finished);
        }

        private void _worker_Finished(object sender, EventArgs e)
        {
            result = _worker.GetResult();
            Console.WriteLine("Thread finished.");
            Start();
        }

        public void Start()
        {
            if (startNewThread)
                _worker.Start();
        }

        public void StopSearching()
        {
            startNewThread = false;
            _worker.Dispose();
        }

        public State GetResult()
        {
            return result;
        }
    }
}
